<?php
//PingBuWang.Com
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\user\controller;

use mip\AdminApiBase;

class ApiAdminUserGroup extends AdminApiBase
{
    public function index(){

    }

    public function getGroupInfo(){
        $g = db('users_group')->where('group_code',input('post.group_code'))->find();
        return jsonSuccess('获取数据成功',$g);
    }

    public function groupList(){
        $list = db('users_group')->order('sort')->select();
        return jsonSuccess('获取数据成功',$list);
    }
    public function getgroupList(){
        $list = db('users_group')->order('sort')->select();
        foreach ($list as $k=>$v){
            $list[$k]['label']=$list[$k]['name'];
            $list[$k]['value']=$list[$k]['group_code'];
        }
        return jsonSuccess('获取数据成功',$list);
    }
    public function groupSortSave(){
        $groupSort = input('post.groupList/a');
        foreach ($groupSort as $key=>$val){
            db('users_group')->where('group_id',$val['group_id'])->update($val);
        }

        return jsonSuccess("排序修改成功");

    }

    public function groupSave(){
        $group_id = input('post.group_id');
        $name = input('post.name');
        $group_code= input('post.group_code');
        $sort= input('post.sort');
        $status= input('post.status');
        $save_type= input('post.save_type');

        if(empty($name)){
            return jsonError('用户组名称必须填写');
        }
        if(empty($group_code)){
            return jsonError('用户组Code必须填写');
        }

        $group=['name'=>$name,'group_code'=>$group_code,'sort' => $sort ,'status' =>$status];

        if($save_type=='edit'){
            db('users_group')->where('group_id',$group_id)->update($group);
        }else  if($save_type=='new'){
            db('users_group')->insert($group);
        }else{
            return jsonError('操作类型异常。save_type:'.$save_type);
        }

        return jsonSuccess('操作成功');






    }


}